package com.resouxs.free_book.service;

import java.io.UnsupportedEncodingException;
import java.util.List;

import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Function;
import retrofit2.http.HEAD;

/**
 * Created by Garrett on 2018/8/30.
 * contact me krouky@outlook.com
 */
public class InterfaceTest {

    public static void main(String[] args) {
        ZSResouService service = (ZSResouService) ApiImp.getInstance().getResouService(ZSResouService.class);
        service.getZsId("大主宰", "天蚕土豆").subscribe(new Observer<Result<List<ZsBookId>>>() {
            @Override
            public void onSubscribe(Disposable d) {
                System.out.println("onSubscribe -- > ");
            }

            @Override
            public void onNext(Result<List<ZsBookId>> value) {
                System.out.println("onNext -- > " + value.getMsg());
                System.out.println("onNext -- > " + value.getData().toString());
                List<ZsBookId> zsBookIdList = value.getData();
                System.out.println("onNext -- > " + zsBookIdList.toString());
                ZsBookId zsBookId = zsBookIdList.get(0);
                String bookId = zsBookId.getZsId();
                getBookSource(bookId);
            }

            @Override
            public void onError(Throwable e) {
                System.out.println("onError -- > "+e.getMessage());
            }

            @Override
            public void onComplete() {
                System.out.println("onComplete -- > ");
            }
        });
    }


    private static void getBookSource(String bookId)
    {
        final String latestChapter = "";
        final ZhuishuService zhuishuService = ApiImp.getInstance().getService(ZhuishuService.class);
        Observable<List<BookSource>> bookSource = zhuishuService.getBookSource(bookId);
        bookSource.flatMap(new Function<List<BookSource>, ObservableSource<ZsBookChapter>>() {
            @Override
            public ObservableSource<ZsBookChapter> apply(List<BookSource> bookSources) throws Exception {
                System.out.println("getBookSource apply -- > " + bookSources.toString());
                float maxSmilarity = 0;
                String sourceId = "";
                for(BookSource bookSource : bookSources)
                {
                    String source = bookSource.getSource();
                    if(source != null && source.contains("vip"))
                    {
                        continue;
                    }

                    if(source != null)
                    {
                        float similarity = SimilarityUtils.levenshtein(bookSource.getLastChapter(), latestChapter);
                        System.out.println("similarity --> "+similarity);

                        if(similarity > maxSmilarity)
                        {
                            maxSmilarity = similarity;
                            sourceId = bookSource.get_id();
                        }
                    }
                }
                if(maxSmilarity < 0.39 && bookSources.size() > 1)
                {
                    sourceId = bookSources.get(1).get_id();
                }
                return zhuishuService.getBookChapter(sourceId, "chapters");
            }
        }).flatMap(new Function<ZsBookChapter, ObservableSource<ZsBookChapterContent>>() {
            @Override
            public ObservableSource<ZsBookChapterContent> apply(ZsBookChapter zsBookChapter) throws Exception {
                System.out.println("getBookSource apply -- > " + zsBookChapter.toString());

                List<ZsChapter> chapters = zsBookChapter.getChapters();

                int latestChIndex = chapters.size() - 1;
                ZsChapter zsChapter = chapters.get(latestChIndex);
                ZSChapterContentService chContentService = ApiImp.getInstance().getChContentService(ZSChapterContentService.class);
                return chContentService.getChapterContent(zsChapter.getLink());
            }
        }).subscribe(new Observer<ZsBookChapterContent>() {
            @Override
            public void onSubscribe(Disposable d) {
                System.out.println("getBookSource onSubscribe -- > ");

            }

            @Override
            public void onNext(ZsBookChapterContent value) {
                System.out.println("getBookContent onNext -- > " + value.toString());
            }

            @Override
            public void onError(Throwable e) {
                System.out.println("getBookSource onError -- > "+e.getMessage());
            }

            @Override
            public void onComplete() {
                System.out.println("getBookSource onComplete -- > ");

            }
        });
    }


    private static void getBookChapters(String sourceId)
    {
        ZhuishuService zhuishuService = ApiImp.getInstance().getService(ZhuishuService.class);
        Observable<ZsBookChapter> chapters = zhuishuService.getBookChapter(sourceId, "chapters");
        chapters.subscribe(new Observer<ZsBookChapter>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(ZsBookChapter value) {
                System.out.println("getBookChapters onNext -- > " + value.toString());

                List<ZsChapter> chapters = value.getChapters();

                int latestChIndex = chapters.size() - 1;
                ZsChapter zsChapter = chapters.get(latestChIndex);
                try {
                    getBookContent(zsChapter.getLink());
                } catch (UnsupportedEncodingException e) {
                    System.out.print(e.getMessage());
                }

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        });
    }


    private static void getBookContent(String bookLink) throws UnsupportedEncodingException {
        ZSChapterContentService chContentService = ApiImp.getInstance().getChContentService(ZSChapterContentService.class);
        Observable<ZsBookChapterContent> chapterContent = chContentService.getChapterContent(bookLink);
        chapterContent.subscribe(new Observer<ZsBookChapterContent>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(ZsBookChapterContent value) {
                System.out.println("getBookContent onNext -- > " + value.toString());
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        });
    }


}
